package com.example.myandroidtest.ui.bluetooth;

import android.Manifest;
import android.app.ComponentCaller;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothManager;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import com.example.myandroidtest.databinding.ActivityBluetoothBinding;

import java.util.Set;

public class BluetoothActivity extends AppCompatActivity {

    ActivityBluetoothBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityBluetoothBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        initView();
    }

    void initView() {
        binding.toolbar.ivBack.setOnClickListener(v -> finish());
        binding.toolbar.tvTitle.setText("网络请求");

        binding.btnScan.setOnClickListener(v -> {
            onBlue();
        });
    }

    void onBlue() {
        //得到bluetoothAdapter
        final BluetoothManager bluetoothManager =
                (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
        BluetoothAdapter adapter = bluetoothManager.getAdapter();
        if (adapter == null) {
            Toast.makeText(this, "该手机没有蓝牙设备", Toast.LENGTH_LONG).show();
            return;
        }
        //判断蓝牙是否打开
        if (!adapter.isEnabled()) {
            Toast.makeText(this, "蓝牙设备未打开", Toast.LENGTH_LONG).show();
            return;
        }

        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) {
            Toast.makeText(this, "没有权限", Toast.LENGTH_LONG).show();

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
                ActivityCompat.requestPermissions(this,
                        new String[]{Manifest.permission.BLUETOOTH_CONNECT}, 1);
            }
            return;
        } else {
            Toast.makeText(this, "有权限", Toast.LENGTH_LONG).show();
        }

        Set<BluetoothDevice> bondedDevices = adapter.getBondedDevices();
        for (BluetoothDevice device : bondedDevices) {
            Log.e("WWW", "设备名称：" + device.getName() + "  设备地址：" + device.getAddress());
        }

        boolean b = adapter.startDiscovery();
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
                                           @NonNull int[] grantResults, int deviceId) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults, deviceId);
        if (requestCode == 1) {
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                onBlue();
            }
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data,
                                 @NonNull ComponentCaller caller) {
        super.onActivityResult(requestCode, resultCode, data, caller);
    }
}